<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="40%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="所属图书馆" prop="library_id">
        <el-select 
          v-model="form.library_id" 
          placeholder="请选择图书馆" 
          style="width: 100%"
          @change="handleLibraryChange"
        >
          <el-option
            v-for="item in libraryOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属楼层" prop="floor_id">
        <el-select 
          v-model="form.floor_id" 
          placeholder="请选择楼层" 
          style="width: 100%"
        >
          <el-option
            v-for="item in floorOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="区域名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入区域名称"></el-input>
      </el-form-item>
      <el-form-item label="容量" prop="capacity">
        <el-input-number v-model="form.capacity" :min="0" :step="1"></el-input-number>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input 
          v-model="form.description" 
          type="textarea" 
          :rows="4"
          placeholder="请输入区域描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from '@/api'

export default {
  name: 'LibraryAreaAdd',
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增区域",
      isEdit: false,
      form: {
        id: undefined,
        library_id: '',
        floor_id: '',
        name: '',
        capacity: 0,
        description: '',
        status: 1
      },
      rules: {
        library_id: [
          { required: true, message: '请选择所属图书馆', trigger: 'change' }
        ],
        floor_id: [
          { required: true, message: '请选择所属楼层', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入区域名称', trigger: 'blur' }
        ],
        capacity: [
          { required: true, message: '请输入容量', trigger: 'blur' }
        ]
      },
      libraryOptions: [],
      floorOptions: []
    }
  },
  created() {
    this.getLibraries()
  },
  methods: {
    getLibraries() {
      http.libraries.list().then(res => {
        this.libraryOptions = res.data
      })
    },
    handleLibraryChange() {
      this.form.floor_id = ''
      this.floorOptions = []
      if (this.form.library_id) {
        http.libraryFloors.list({ library_id: this.form.library_id }).then(res => {
          this.floorOptions = res.data
        })
      }
    },
    open(row) {
      this.dialogVisible = true;
      if (row) {
        this.isEdit = true;
        this.dialogTitle = "编辑区域";
        this.form = { ...row };
        // 加载楼层选项
        if (this.form.library_id) {
          this.handleLibraryChange();
        }
      } else {
        this.isEdit = false;
        this.dialogTitle = "新增区域";
        this.form = {
          id: undefined,
          library_id: '',
          floor_id: '',
          name: '',
          capacity: 0,
          description: '',
          status: 1
        };
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              await http.libraryAreas.update(this.form.id, this.form);
              this.$message.success("更新成功");
            } else {
              await http.libraryAreas.create(null,this.form);
              this.$message.success("新增成功");
            }
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            if (error.response && error.response.data && error.response.data.message) {
              this.$message.error(error.response.data.message);
            } else {
              this.$message.error("操作失败，请检查区域信息是否重复");
            }
          }
        }
      });
    }
  }
}
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
</style>
